package com.xdtech.hotel.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.DeviceLinkOptionDao;
import com.xdtech.hotel.model.DeviceLinkOption;
import com.xdtech.hotel.service.DeviceLinkOptionService;
import com.xdtech.hotel.vo.DeviceLinkOptionItem;
import com.xdtech.sys.model.CodeValue;
import com.xdtech.sys.vo.CodeValueItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:13:09
 * @since 1.0
 * @see
 */
@Service
public class DeviceLinkOptionServiceImpl implements DeviceLinkOptionService {
	@Autowired
	private DeviceLinkOptionDao deviceLinkOptionDao;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param entity
	 */
	public void save(DeviceLinkOption entity) {
		deviceLinkOptionDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<DeviceLinkOption> entities) {
		deviceLinkOptionDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param entity
	 */
	public void delete(DeviceLinkOption entity) {
		deviceLinkOptionDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		deviceLinkOptionDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param id
	 * @return
	 */
	public DeviceLinkOption get(Long id) {
		return deviceLinkOptionDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @return
	 */
	public List<DeviceLinkOption> getAll() {
		return deviceLinkOptionDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> deviceLinkOptions = null;
		long rows = 0;
		if (pg!=null) {
			Page<DeviceLinkOption> page = deviceLinkOptionDao.findPage(pg,"from DeviceLinkOption order by createTime desc", values);
			deviceLinkOptions = BeanUtils.copyListProperties(
					DeviceLinkOptionItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<DeviceLinkOption> deviceLinkOptionList = deviceLinkOptionDao.find("from DeviceLinkOption order by id desc", values);
			deviceLinkOptions = BeanUtils.copyListProperties(
					DeviceLinkOptionItem.class, deviceLinkOptionList);
			rows = deviceLinkOptions.size();
		}
		results.put("total", rows);
		results.put("rows", deviceLinkOptions);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateDeviceLinkOption(DeviceLinkOptionItem item) {
		DeviceLinkOption deviceLinkOption = null;
		if (item.getId()==null) {
			deviceLinkOption = new DeviceLinkOption();
		}else {
			deviceLinkOption = deviceLinkOptionDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(deviceLinkOption, item);
		deviceLinkOptionDao.save(deviceLinkOption);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param deviceLinkOptionId
	 * @return
	 */
	public DeviceLinkOptionItem loadDeviceLinkOptionItem(Long deviceLinkOptionId) {
		DeviceLinkOption deviceLinkOption = deviceLinkOptionDao.get(deviceLinkOptionId);
		DeviceLinkOptionItem deviceLinkOptionItem = new DeviceLinkOptionItem();
		BeanUtils.copyProperties(deviceLinkOptionItem, deviceLinkOption);
		return deviceLinkOptionItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteDeviceLinkOptionInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:13:09
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteDeviceLinkOptionInfo(List<Long> deviceLinkOptionIds) {
		for (Long id : deviceLinkOptionIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-5下午1:03:04
	 * @modified by
	 * @param pg
	 * @param deviceLinkId
	 * @return
	 */
	@Override
	public Map<String, Object> loadByDeviceLinkId(Pagination pg,
			Long deviceLinkId) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page<DeviceLinkOption> page = deviceLinkOptionDao.findPage(pg,"from DeviceLinkOption where deviceLink.id=?",deviceLinkId);
		results.put("total", page.getTotalItems());
		results.put("rows", DeviceLinkOptionItem.createItems(page.getResult()));
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-5下午3:34:08
	 * @modified by
	 * @param id
	 */
	@Override
	public void deleteByLinkId(Long linkId) {
		deviceLinkOptionDao.batchExecute("delete from DeviceLinkOption where deviceLink.id=?", linkId);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-9下午11:28:16
	 * @modified by
	 * @param id
	 * @return
	 */
	@Override
	public List<DeviceLinkOptionItem> loadDeviceLinkOptionsByLinkId(Long id) {
		List<DeviceLinkOption> deviceLinkOptions = deviceLinkOptionDao.findByHql("from DeviceLinkOption where deviceLink.id=?", id);
		return DeviceLinkOptionItem.createItems(deviceLinkOptions);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:16:52
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

}
